import CryptoJS from "crypto-js";
//小程序sdk vod-wx-sdk-v2
const VodUploader = require("@/static/js/vod-wx-sdk-v2.js");
export default {
  // 微信小程序获取签名---写法必须用wx.request否则云点播不识别
  getSignature: function (callback) {
    let token = uni.getStorageSync("token");
    wx.request({
      url: `${process.env.UNI_BASE_URL}/api/common/material/signature`,
      header: {
        Authorization: "Bearer" + token,
      },
      success: function (res) {
        console.log("res", res.data.data.signature);
        const key = CryptoJS.MD5(token).toString();
        // 根据key获取解密iv
        const iv = key.slice(0, 16);
        const keyParse = CryptoJS.enc.Utf8.parse(key);
        const ivParse = CryptoJS.enc.Utf8.parse(iv);
        // 解密
        const decrypted = CryptoJS.AES.decrypt(
          res.data.data.signature,
          keyParse,
          {
            iv: ivParse,
            mode: CryptoJS.mode.CFB,
            padding: CryptoJS.pad.NoPadding,
          }
        );
        const sign = decrypted.toString(CryptoJS.enc.Utf8);
        console.log("22222", sign);
        callback(sign);
      },
    });
  },
  //   根据签名获取结果url
  getUrl(files) {
    // 想要获取的结果
    let VodUploaderIns = {};
    let promise = new Promise((resolve, reject) => {
      VodUploader.start({
        // 必填，把 wx.chooseVideo 回调的参数(file)传进来
        mediaFile: files[0],
        // 必填，获取签名的函数
        //   getSignature: test.getSignature,
        getSignature: this.getSignature,
        // 选填，视频名称，强烈推荐填写(如果不填，则默认为“来自小程序”)
        mediaName: "猎手",
        // 选填，视频封面，把 wx.chooseImage 回调的参数(file)传进来
        //   coverFile: coverFile,
        // 上传中回调，获取上传进度等信息
        progress: function (result) {
          console.log("progress", result);
          VodUploaderIns = result;
          VodUploaderIns.type = "progress";
          // resolve(VodUploaderIns);
        },
        // 上传完成回调，获取上传后的视频 URL 等信息
        finish: function (result) {
          console.warn("finish", result);
          VodUploaderIns = result;
          VodUploaderIns.type = "finish";
          resolve(VodUploaderIns);
        },
        // 上传错误回调，处理异常
        error: function (result) {
          console.error("error", result);
          VodUploaderIns = result;
          VodUploaderIns.type = "error";
          reject(false);
        },
      });

    });
    return promise;
   
  },
};
